package com.szboanda.platform.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadUtils {
    
    private HttpServletRequest request;
    
    private String encode;
    
    private FileItemFactory factory;
    private ServletFileUpload upload;
    private ProgressListener pl;
    
    private Map<String, List<String>> parameterValues = new HashMap<String, List<String>>(0);
    private Map<String, List<FileItem>> fileItems = new HashMap<String, List<FileItem>>(0);
    
    public UploadUtils(HttpServletRequest request) {
        this.request = request;
        this.init();
    }
    
    public UploadUtils(HttpServletRequest request, String encode) {
        this.request = request;
        this.encode = encode;
        this.init();
    }
    
    public UploadUtils(HttpServletRequest request, ProgressListener pl) {
        this.request = request;
        this.pl = pl;
        this.init();
    }
    
    public UploadUtils(HttpServletRequest request, String encode, ProgressListener pl) {
        this.request = request;
        this.encode = encode;
        this.pl = pl;
        this.init();
    }
    
    @SuppressWarnings("unchecked")
    private void init() {
        boolean isMultipart = ServletFileUpload.isMultipartContent(this.request);
        if (isMultipart) {
            try {
                this.factory = new DiskFileItemFactory();
                this.upload = new ServletFileUpload(this.factory);
                this.upload.setProgressListener(this.pl);

                // 获取页面所有的参数
                List<FileItem> items = this.upload.parseRequest(this.request);
                
                if (null != items && 0 < items.size()) {
                    for (FileItem item : items) {
                        String name = item.getFieldName();
                        
                        if (item.isFormField()) {
                            List<String> values = this.parameterValues.get(name);
                            if (null == values) {
                                values = new ArrayList<String>(0);
                            }
                            if (StringUtils.isNotBlank(encode)) {
                                try {
                                    values.add(item.getString(encode));
                                } catch (UnsupportedEncodingException e) {
                                    e.printStackTrace();
                                }
                            } else {
                                values.add(item.getString());
                            }
                            this.parameterValues.put(name, values);
                            
                        } else  {// 文件
                            List<FileItem> files = this.fileItems.get(name);
                            if (null == files) {
                                files = new ArrayList<FileItem>(0);
                            }
                            files.add(item);
                            this.fileItems.put(name, files);
                        }
                    }
                }
                
            } catch (FileUploadException e) {
                e.printStackTrace();
            }
        }
    }
    
    public String getParameter(String name) {
        String[] values = this.getParameterValues(name);
        if (null != values && 0 < values.length) {
            return values[0];
        }
        return null;
    }
    
    public String[] getParameterValues(String name) {
        List<String> values = this.parameterValues.get(name);
        if (null == values || values.isEmpty()) {
            return null;
        }
        return values.toArray(new String[values.size()]);
    }
    
    public FileItem getFileItem(String name) {
        FileItem[] files = this.getFileItems(name);
        if (null != files && 0 < files.length) {
            return files[0];
        }
        return null;
    }
    
    public FileItem[] getFileItems(String name) {
        List<FileItem> files = this.fileItems.get(name);
        if (null == files || files.isEmpty()) {
            return null;
        }
        return files.toArray(new FileItem[files.size()]);
    }
}
